#include <WinSock2.h>
#include "Base.h"
#include "DB/AsynDBopt.h"
#include "DB/DBConnection.h"
#include "WorldServer.h"


DBQueryItem::DBQueryItem( mysqlpp::TCPConnection* _pCon,mysqlpp::Query& _query,AsynDB_CallBack* pCallBack ):m_query(_query)
{
	m_pCon = _pCon;
	m_pCallBack = pCallBack;
}
void DBQueryItem::Run()
{
DB_TRY
	int index = 0;
    mysqlpp::StoreQueryResult reslut = m_query.store();
    m_pCallBack->setResult(index++,reslut);
    while (m_query.more_results())
    {
    	reslut = m_query.store_next();
    	m_pCallBack->setResult(index++,reslut);
    }
	
	g_WorldServer->addFinishedDBopt(m_pCallBack);
DB_CATCH_0
}



AsynDB_CallBack::~AsynDB_CallBack()
{
	
}



AsynDB_CallBack::AsynDB_CallBack()
{

}

void AsynDB_CallBack::setResult(int index, const mysqlpp::StoreQueryResult& _result )
{
	MY_ASSERT0(index < Result_Count && index >= 0);
	m_result[index] = _result;
}


void DBWorkQueue::WorkRoutine()
{
	mysqlpp::Connection::thread_start();

	WorkQueue::WorkRoutine();

	mysqlpp::Connection::thread_end();
}

DBTemplateQuery::DBTemplateQuery( mysqlpp::TCPConnection* _pCon,mysqlpp::Query& _query,AsynDB_CallBack* pCallBack,const mysqlpp::SQLQueryParms& str )
	:DBQueryItem(_pCon,_query,pCallBack),m_queryParms(str)
{

}

void DBTemplateQuery::Run()
{
DB_TRY
	int index = 0;
	mysqlpp::StoreQueryResult reslut = m_query.store(m_queryParms);
	m_pCallBack->setResult(index++,reslut);
	while (m_query.more_results())
	{
		reslut = m_query.store_next();
		m_pCallBack->setResult(index++,reslut);
	}
	
	g_WorldServer->addFinishedDBopt(m_pCallBack);
DB_CATCH_0
}
